
const state = {
	productList: [],
	priceTotal: 0,
	addressInformation: {}
}

const mutations = {
  SET_PRODUCTLIST: (state, productList) => {
    state.productList = productList
  },
  SET_PRICETPTAL: (state, priceTotal) => {
    state.priceTotal = priceTotal
  },
  SET_ADDRESSINFORMATION: (state, addressInformation) => {
    state.addressInformation = addressInformation
  },
}

const actions = {
  // add product
	addProduct({commit, state},info) {
		return new Promise (resolve => {
			let list = state.productList;
			let price = state.priceTotal;
			list.push(info);
			// console.log(price + Number(info.price))
			commit('SET_PRICETPTAL', price + Number(info.price));
			commit('SET_PRODUCTLIST', list);
		})
	},
	// delete product
	deleProduct({commit, state},id) {
		return new Promise (resolve => {
			let list = state.productList, arry = [], price = 0;
			for (let i in list) {
				if (list[i].id !== id) {
					arry.push(list[i])
					price += Number(list[i].price);
				}
			}
			commit('SET_PRICETPTAL', price);
			commit('SET_PRODUCTLIST', arry);
		})
	}
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
